<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="选择客户" prop="customerId">
        <el-select v-model="queryParams.customerId" placeholder="请选择客户" style="width: 240px" @change="getProList">
          <el-option v-for="i in userListOptions" :key="i.userId" :label="i.nickName" :value="i.userId" />
        </el-select>
      </el-form-item>
      <el-form-item label="选择项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请先选择客户" style="width: 240px">
          <el-option v-for="i in proListOptions" :key="i.projectId" :label="i.projectName" :value="i.projectId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :search="false"> </right-toolbar> -->
      <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
    </el-row>

    <el-table :data="cityOptions" style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table :data="props.row.subPlanList" style="width: 100%; margin-bottom: 20px">
            <el-table-column prop="cycleNo" label="期数"></el-table-column>
            <el-table-column prop="numberOfMonthlyInspectionTasks" label="月任务"></el-table-column>
            <el-table-column prop="numberOfQuarterlyInspectionTasks" label="季任务"></el-table-column>
            <el-table-column prop="numberOfSemiAnnualInspectionTasks" label="半年任务"></el-table-column>
            <el-table-column prop="numberOfAnnualInspectionTasks" label="年任务"></el-table-column>
            <el-table-column prop="numberOfBienniallyInspectionTasks" label="两年任务"></el-table-column>
            <el-table-column prop="totalCompleted" label="已完成"></el-table-column>
            <el-table-column prop="incomplete" label="未完成"></el-table-column>
            <el-table-column prop="operatorName" label="中级消防设施操作员"></el-table-column>
            <el-table-column prop="engineerName" label="一级消防注册工程师"></el-table-column>
            <el-table-column prop="approverName" label="批准人"></el-table-column>
            <el-table-column prop="totalCompleted" label>
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-view" size="mini" @click="handleDetail(scope.row.id)"
                  style="background: #999; border: none">详情查看</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column prop="customerName" label="客户名称"></el-table-column>
      <el-table-column prop="projectName" label="项目名称"></el-table-column>
      <el-table-column prop="numberOfTasks" label="任务总数"></el-table-column>
      <el-table-column prop="beginTime" label="开始时间"></el-table-column>
      <el-table-column prop="endTime" label="结束时间"></el-table-column>
      <el-table-column prop="status" label="任务状态">
        <template slot-scope="scope">
          {{ scope.row.status == 1 ? "已开始" : "已结束" }}
        </template>
      </el-table-column>
      <el-table-column prop="projectStatus" label="项目状态">
        <template slot-scope="scope">
          {{ scope.row.projectStatus == 3 ? "正常" : scope.row.projectStatus == 5 ? "终止" : '' }}
        </template>
      </el-table-column>
      <!-- 总数减 -->
      <el-table-column prop="incomplete" label="未完成"></el-table-column>
      <!-- <el-table-column prop="address" label="操作"></el-table-column> -->
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <Detail v-if="detailVisible" ref="Detail" @refreshDetail="refreshDetail" />
    <editFloorList v-if="editFloorVisible" ref="Detail" @refresh="editFloorVisible = false" />
    <taskDetail v-if="taskDetailVisible" ref="taskDetail"></taskDetail>
  </div>
</template>

<script>
import {
  getUserList,
  getProjectPlanByCustomerId,
  getPlanList,
  getListByCustomerId,
  exPortPlanList,
} from "@/api/plan-produce/task";
import { tansParams, blobValidate } from "@/utils/ruoyi";
import Detail from "./detail.vue";
import editFloorList from "./editFloor.vue";
import taskDetail from "./task-detail.vue";

export default {
  name: "Building",
  components: { Detail, editFloorList, taskDetail },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 客户列表
      userList: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 建筑表格数据
      buildingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 详情页面
      detailVisible: false,
      // 编辑楼层
      editFloorVisible: false,
      taskDetailVisible: false,
      // 客户列表
      userListOptions: [],
      proListOptions: [],
      cityOptions: [],

      // 查询参数
      queryParams: {
        customerId: null,
        projectId: null,
        pageNum: 1,
        pageSize: 10,
      },
      cycleNoOptions: [
        { fullName: "一", id: 1 },
        { fullName: "二", id: 2 },
        { fullName: "三", id: 3 },
        { fullName: "四", id: 4 },
        { fullName: "五", id: 5 },
        { fullName: "六", id: 6 },
        { fullName: "七", id: 7 },
        { fullName: "八", id: 8 },
        { fullName: "九", id: 9 },
      ],
      statusOptions: [
        { fullName: "未预约", id: 1 },
        { fullName: "已预约", id: 2 },
        { fullName: "执行中", id: 3 },
        { fullName: "已完成", id: 4 },
        { fullName: "未完成", id: 5 },
        { fullName: "已完结", id: 6 },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceOrgId: [
          {
            required: true,
            message: "技术服务机构id不能为空",
            trigger: "blur",
          },
        ],
        customerId: [
          { required: true, message: "客户不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },

  methods: {
    getList() {
      this.loading = true;
      // 获取用户列表
      getUserList("02").then((response) => {
        this.userListOptions = response.data;
        this.loading = false;
      });
      getPlanList(this.queryParams).then((res) => {
        this.cityOptions = res.rows;
        this.total = res.total;
      });
    },
    // 查询项目
    getProList() {
      getListByCustomerId(this.queryParams.customerId).then((res) => {
        this.proListOptions = res.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serviceOrgId: null,
        customerId: null,
        name: null,
        address: null,
        pointPositionDesc: null,
        abovegroundNumber: null,
        undergroundNumber: null,
        longitude: null,
        latitude: null,
        invalid: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      exPortPlanList({
        customerId: this.queryParams.customerId,
        projectId: this.queryParams.projectId,
      }).then(async (data) => {
        console.log(data, ">>>56555");
        const isBlob = blobValidate(data);
        const filename = ` task_${new Date().getTime()}.xlsx`;
        if (isBlob) {
          saveAs(data.data, filename);
        } else {
          const resText = await data.text();
          const rspObj = JSON.parse(resText);
          const errMsg =
            errorCode[rspObj.code] || rspObj.msg || errorCode["default"];
          this.$modal.msgError(errMsg);
        }
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.proListOptions = []
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 跳转详情
    handleDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    refreshDetail(list) {
      this.detailVisible = false;
      setTimeout(() => {
        this.taskDetailVisible = true;
        this.$nextTick(() => {
          this.$refs.taskDetail.init(list);
        });
      }, 0);
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBuilding(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuilding(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped rel="stylesheet/scss">
.mapSty {
  width: 100%;
  height: 450px;
  position: relative;
}

#allMap {
  width: 100%;
  height: 450px;
}

.textSty {
  font-size: 14px;
  color: rgb(102, 102, 102);
}

.textIconSty {
  margin-left: 10px;
  font-size: 12px;
  color: rgb(102, 102, 102);
  margin-right: 0.3rem;
  // flex: 1;

  .listIcon {
    padding: 0 5px;
  }
}

.text-with-line {
  text-decoration: line-through;
}

::v-deep .el-card__body {
  padding: 0;
}
</style>
